package edu.serg.mbar.algorithms.functions;

import java.util.Vector;

import org.apache.log4j.Logger;

import edu.serg.mbar.ucon.interfaces.IFunction;

/**
 * This class is representative of a function for calculating sum of different values
 * 
 * @author Sanaullah
 *
 */
		
public class Add implements IFunction {
	static Logger log = Logger.getLogger(Add.class);

	@Override
	/**
	 *This functions evaluates the sum of all the elements supplied to the function as an argument in the form of a vector of Objects
	 * 
	 *@param
	 *		The vector of objects whose sum is to be calculated
	 *
	 */
	public Object evaluate(Vector<Object> param) {
		log.debug("*Start adding " + param.size() + "elements");
		double result =0;
		int totalElements =param.size();
		for (int i=0; i < totalElements ; i++){
			Object ithElement = param.elementAt(i);
			log.debug("the value of first element is "+ (String) ithElement);
			ithElement = Double.valueOf((String) ithElement).doubleValue();
			result =result + (Double) ithElement;
		}

		log.debug("*The result of the summation is "+ result);
		return result;
	}

}
